package com.zhitong.money.mapper;

import com.zhitong.money.entity.Message;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: Andy Zhang
 * @Description:
 * @Date: 15:30 2021/2/25
 * @Company: wisecard
 */
@Mapper
public interface MessageMapper {

    @Results(id = "msgMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "tid",property = "tid"),
            @Result(column = "name",property = "name"),
            @Result(column = "type",property = "type"),
            @Result(column = "title",property = "title"),
            @Result(column = "html",property = "html"),
            @Result(column = "content",property = "content"),
            @Result(column = "createTime",property = "createTime"),
            @Result(column = "status",property = "status")
    })
    @Select("select employee.name,message.* from employee,message where tid=tellerId")
    public List<Message> findAllMsg();

    @ResultMap("msgMap")
    @Select("select employee.name,message.* from employee,message where tid=tellerId and message.id=#{id}")
    public Message findMsgById(String id);

//    @ResultMap("msgMap")
//    @Select("select * from message where id in (select mid  from message_employee where tid = #{tid})")

    @ResultMap("msgMap")
    @Select({
            "<script>",
            "select employee.name,message.* from employee,message",
            "<where>",
            "and tid=tellerId",
            "<if test='tid!=null and tid!=\"\"'>and tid=#{tid}</if>",
            "<if test='name!=null and name!=\"\"'>and name like concat(concat('%',#{name},'%'))</if>",
            "<if test='type!=null and type!=\"\"'>and type=#{type}</if>",
            "<if test='title!=null and title!=\"\"'>and title=#{title}</if>",
            "<if test='status!=null and status!=\"\"'>and message.status=#{status}</if>",
            "<if test='html!=null and html!=\"\"'>and html=#{html}</if>",
            "<if test='content!=null and content!=\"\"'>and content=#{content}</if>",
            "<if test='createTime!=null'>and createTime=#{createTime}</if>",
            "</where>",
            "</script>"
    })
    public List<Message> findMsgByMap(Map<String,Object> map);

    @Insert("insert into message values(#{id},#{tid},#{type},#{title},#{html},#{content},Now(),'01')")
    public void addMsg(Map<String,Object> map);

    @Update({
            "<script>",
            "update message",
            "<trim prefix='set' suffixOverrides=','>",
            "<if test='tid!=null and tid!=\"\"'>tid=#{tid},</if>",
            "<if test='type!=null and type!=\"\"'>type=#{type},</if>",
            "<if test='title!=null and title!=\"\"'>title=#{title},</if>",
            "<if test='html!=null and html!=\"\"'>html=#{html},</if>",
            "<if test='content!=null and content!=\"\"'>content=#{content},</if>",
            "<if test='status!=null and status!=\"\"'>status=#{status},</if>",
            "</trim>",
            "where id = #{id}",
            "</script>"
    })
    public void updateMsg(Message message);

    @Update("update message set status=#{status} where id= #{id}")
    public void updateStatus(String id,String status);

    @Delete("delete from message where id = #{id}")
    public void delMsgById(String id);

    @Delete("delete from message where tid=#{tid}")
    public void delMsgByTid(String tid);

    @Delete({
            "<script>",
            "delete from message",
            "where id in",
            "<foreach item='item' collection='ids' separator=',' open='(' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public void delMsgByIds(List<String> ids);

    @Delete({
            "<script>",
            "delete from message",
            "where tid in",
            "<foreach item='item' collection='tids' separator=',' open='(' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public void delMsgByTids(List<String> tids);






}
